home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / unix / mp14tar.z / mp14tar / mpack / macninit.c < prev    next >
C/C++ Source or Header  |  1994-06-01  |  5KB  |  192 lines

  1. /* macninit.c -- general mac nifty application library initialization
  2.  *
  3.  * (C) Copyright 1990-1993 by Christopher J. Newman
  4.  * All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, distribute, and sell this software and its
  7.  * documentation for any purpose is hereby granted without fee, provided that
  8.  * the above copyright notice appear in all copies and that both that
  9.  * copyright notice and this permission notice appear in supporting
  10.  * documentation, and that the name of Christopher J. Newman not be used in
  11.  * advertising or publicity pertaining to distribution of the software without
  12.  * specific, written prior permission.  Christopher J. Newman makes no
  13.  * representations about the suitability of this software for any purpose.  It
  14.  * is provided "as is" without express or implied warranty.
  15.  *
  16.  * CHRISTOPHER J. NEWMAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  17.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT
  18.  * SHALL CHRISTOPHER J. NEWMAN BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  19.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  20.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  21.  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22.  * OF THIS SOFTWARE.
  23.  *
  24.  * Author:    Christopher J. Newman
  25.  * Message:    This is a nifty program.
  26.  */
  27.  
  28. #ifndef THINK_C
  29. #include <Fonts.h>
  30. #include <SegLoad.h>
  31. #include <OSEvents.h>
  32.  
  33. extern void _DataInit();            /* MPW initialize routine */
  34. #endif
  35. #include <Traps.h>
  36. #include <AppleEvents.h>
  37. #include <GestaltEqu.h>
  38. #include "macnapp.h"
  39.  
  40. extern pascal OSErr NArequiredAE(AppleEvent *, AppleEvent *, long);
  41.  
  42. #ifndef _Unimplemented
  43. #define _Unimplemented                    0xA89F
  44. #endif
  45. #ifndef _WaitNextEvent
  46. #define _WaitNextEvent                    0xA860
  47. #endif
  48. #ifndef _Gestalt
  49. #define _Gestalt                        0xA1AD
  50. #endif
  51.  
  52. /* global to hold the application heap zone */
  53. extern THz NAappzone;
  54.  
  55. static Boolean TrapAvailable(short);
  56.  
  57. /* this checks if a given trap is available on the system
  58.  */
  59. static Boolean TrapAvailable(short tNumber)
  60. {
  61.     short numtraps = 0x400;
  62.     TrapType tType;
  63.     
  64.     tType = tNumber & 0x800 ? ToolTrap : OSTrap;
  65.     if (tType == ToolTrap) {
  66.         if (NGetTrapAddress(_InitGraf, ToolTrap)
  67.             == NGetTrapAddress(0xAA6E, ToolTrap)) {
  68.             numtraps = 0x200;
  69.         }
  70.         tNumber = tNumber & 0x07FF;
  71.         if (tNumber >= numtraps)
  72.             tNumber = _Unimplemented;
  73.     }
  74.     
  75.     return (NGetTrapAddress(tNumber, tType)
  76.             != NGetTrapAddress(_Unimplemented, ToolTrap));
  77. }
  78.  
  79. /* initialize the Macintosh managers and niftyapp internal variables.
  80.  * call this first.  You may wish to call MoreMasters() after this.
  81.  */
  82. short NAinit(short minK, short masters, na_openp openp, na_menup menup,
  83.     short numapple, short newitem, short closeitem)
  84.  {
  85.     long        total, contig, response;
  86.     EventRecord event;
  87.     short        count;
  88.     Handle        menuBar;
  89.     short        message, i, aeflag = 0;
  90.     AppFile        afile;
  91.     FSSpec        fspec;
  92.  
  93. #ifndef THINK_C
  94.     UnloadSeg((Ptr) _DataInit);        /* unload MPW init routine */
  95. #endif
  96.     MaxApplZone();                    /* expand heap so code loads at top */
  97.     
  98.     /* initialize all the managers */
  99.     InitGraf((Ptr) &QD(thePort));
  100.     InitFonts();
  101.     InitWindows();
  102.     InitMenus();
  103.     TEInit();
  104.     InitDialogs(NULL);
  105.     InitCursor();
  106.     
  107.     /* allocate enough master pointers */
  108.     for (count = 1; count <= masters; count++) {
  109.         MoreMasters();
  110.         if (MemError() != noErr) return (-1);
  111.     }
  112.     
  113.     /* hack to bring to front in MultiFinder */
  114.     for (count = 1; count <= 3; count++) EventAvail(everyEvent, &event);
  115.         
  116.     /* get the system environment */
  117.     (void) SysEnvirons(1, &NAsysenv);
  118.     
  119.     /* verify we have 128K ROMS, WaitNextEvent is available, and we have enough memory */
  120.     if (NAsysenv.machineType < 0
  121.             || !TrapAvailable(_WaitNextEvent)
  122.             || (long) GetApplLimit() - (long) ApplicZone() < minK) {
  123.         return (-1);
  124.     }
  125.     
  126.     /* check for Gestalt and set up Apple Event Handler */
  127.     NAgestaltBits = 0;
  128.     if (TrapAvailable(_Gestalt)) {
  129.         NAgestaltBits = NA_HASGESTALT;
  130.         if (Gestalt(gestaltAppleEventsAttr, &response) == noErr) {
  131.             NAgestaltBits |= NA_HASAEVENTS;
  132.             if (AEInstallEventHandler(kCoreEventClass, typeWildCard,
  133.                     NArequiredAE, (long) openp, FALSE) == noErr) {
  134.                 aeflag = 1;
  135.             }
  136.         }
  137.     }
  138.     
  139.     /* clean up and check available free memory */
  140.     PurgeSpace(&total, &contig);
  141.     if (total < minK) return (-1);
  142.     
  143.     /* store our application heap zone */
  144.     NAappzone = ApplicZone();
  145.     
  146.     /* if the user wants automatic menu handling, do it now */
  147.     if (menup != (na_menup) NULL) {
  148.         if ((menuBar = GetNewMBar(NA_MBAR)) == (Handle) NULL)
  149.             return (-1);
  150.         SetMenuBar(menuBar);
  151.         DisposHandle(menuBar);
  152.         AddResMenu(GetMHandle(mApple), 'DRVR');
  153.         DrawMenuBar();
  154.         NAcloseitem = closeitem;
  155.         NAnewitem = newitem;
  156.         NAappleitems = numapple;
  157.         NAhasedit = true;
  158.         NAmenup = menup;
  159.     }
  160.     
  161.     /* create full region */
  162.     SetRectRgn(NAfullRgn = NewRgn(), -32767, -32767, 32767, 32767);
  163.     NAnullRgn = NewRgn();
  164.     
  165.     /* save ibeam cursor */
  166.     NAibeam = **GetCursor(iBeamCursor);
  167.     
  168.     /* handle startup files */
  169.     if (!aeflag) {
  170.         count = 0;
  171.         if (openp != (na_openp) NULL) {
  172.             CountAppFiles(&message, &count);
  173.             if (count) {
  174.                 for (i = 1; i <= count; i++) {
  175.                     GetAppFiles(i, &afile);
  176.                     if ((*openp)(message, &afile, 0) < 0) break;
  177.                 }
  178.                 ClrAppFiles(count);
  179.                 if (message == appPrint
  180.                         && NAcloseWindows(NAhead, NA_REQCLOSEALL) == NA_ALLCLOSED) {
  181.                     return (1);
  182.                 }
  183.             }
  184.         }
  185.         if (newitem && !count && menup != (na_menup) NULL) {
  186.             (*menup)(NULL, mFile, newitem);
  187.         }
  188.     }
  189.  
  190.     return (0);
  191. }
  192.